home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / libsrc / c / sys / stat_ast.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-02  |  3.0 KB  |  111 lines

  1. /*****************************************************************************\
  2.  * $Id: stat_ast.c 2.0 1994/03/14 03:22:53 dj Exp $
  3.  * $Log: stat_ast.c $
  4.  * Revision 2.0  1994/03/14  03:22:53  dj
  5.  * initial version
  6.  *
  7. \*****************************************************************************/
  8.  
  9. /* Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
  10.  *
  11.  * This software comes with ABSOLUTELY NO WARRANTY, and may be used and
  12.  * copied freely as long as the copyright is left intact.
  13.  */
  14.  
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <time.h>
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <dir.h>
  22. #include <go32.h>
  23. #include <ctype.h>
  24.  
  25. int stat_assist(const char* filename, struct stat* statbf)
  26. {
  27.   struct ffblk ff;
  28.   int i;
  29.   struct tm tmblk;
  30.   const char *fend = filename + strlen(filename);
  31.  
  32.   if((i = findfirst(filename, &ff, FA_DIREC|FA_HIDDEN|FA_SYSTEM)))
  33.   {
  34.     if (errno == ENMFILE)
  35.       errno = ENOENT;
  36.     return i;
  37.   }
  38.   memset(statbf, 0, sizeof(struct stat));
  39.  
  40.   if(ff.ff_attrib & FA_RDONLY)
  41.     i = S_IREAD;
  42.   else
  43.     i = S_IREAD | S_IWRITE;
  44.   if(ff.ff_attrib & 64)        /* Not a file, device driver or special */
  45.     i |= S_IFCHR;
  46.   else if(ff.ff_attrib & FA_DIREC)
  47.     i |= S_IFDIR | S_IEXEC;
  48.   else
  49.   {
  50.     i |= S_IFREG;
  51.     if (stricmp(fend-4, ".bat") == 0
  52.     || stricmp(fend-4, ".com") == 0)
  53.       i |= S_IEXEC;
  54.     else if (stricmp(fend-2, ".o") == 0
  55.          || stricmp(fend-2, ".c") == 0
  56.          || stricmp(fend-3, ".cc") == 0)
  57.       ;
  58.     else
  59.     {
  60.       unsigned char two[2];
  61.       int fd = open(filename, O_RDONLY|O_BINARY);
  62.       if (fd>=0)
  63.     if (read(fd, two, 2) == 2)
  64.     {
  65.       switch ((two[1] << 8) + two[0])
  66.       {
  67.       case 0x010b:        /* a.out */
  68.       case 0x014c:        /* COFF */
  69.       case 0x2123:        /* #! script */
  70.       case 0x5a4d:        /* EXE */
  71.         i |= S_IEXEC;
  72.         break;
  73.       }
  74.     }
  75.       close(fd);
  76.     }
  77.   }
  78.   statbf->st_mode = i | ((i>>3) & 050) | ((i>>6) & 005) ;
  79.   statbf->st_size = ff.ff_fsize;
  80.   statbf->st_uid = getuid();
  81.   statbf->st_gid = getgid();
  82.   statbf->st_ino = 42; /* dummy */
  83.   statbf->st_nlink = 1;
  84.   statbf->st_blksize = _go32_info_block.size_of_transfer_buffer;
  85.   if(ff.ff_attrib & 64)        /* Not a file, device driver or special */
  86.   {
  87.     statbf->st_dev = statbf->st_rdev = -1;
  88.     statbf->st_mtime = statbf->st_atime = statbf->st_ctime = 0;
  89.     return 0;
  90.   }
  91.   if(filename[1] == ':')
  92.     i = tolower(filename[0]) - 'a';
  93.   else
  94.     i = getdisk();        /* current drive */
  95.   statbf->st_dev = statbf->st_rdev = i;
  96.  
  97.   tmblk.tm_sec = 2 * (ff.ff_ftime & 31);
  98.   tmblk.tm_min = (ff.ff_ftime >> 5) & 63;
  99.   tmblk.tm_hour = (ff.ff_ftime >> 11) & 31;
  100.   tmblk.tm_mday = ff.ff_fdate & 31;
  101.   tmblk.tm_mon = ((ff.ff_fdate >> 5) & 15) - 1;
  102.   tmblk.tm_year = ((ff.ff_fdate >> 9) & 127) + 80;
  103.   tmblk.tm_wday = tmblk.tm_yday = tmblk.tm_isdst = tmblk.tm_gmtoff = 0;
  104.   tmblk.tm_zone = NULL;
  105.   tmblk.tm_isdst = -1;
  106.  
  107.   statbf->st_ctime = mktime(&tmblk);
  108.   statbf->st_mtime = statbf->st_atime = statbf->st_ctime;
  109.   return 0;
  110. }
  111.